package com.wx.mp.controller;

import com.alibaba.fastjson.JSON;
import com.wx.mp.config.WxMpConfiguration;
import com.wx.mp.dao.TCarSubscribeDao;
import com.wx.mp.dao.impl.TCarSubscribeDaoImpl;
import com.wx.mp.request.subscribe.oper.CarSubscribeSaveReq;
import com.wx.mp.request.subscribe.query.QuerySubscribeInfosReq;
import com.wx.mp.request.subscribe.query.QuerySubscribeInfosRes;
import com.wx.mp.utils.BizResult;
import com.wx.mp.utils.BizResultVO;
import com.wx.mp.utils.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpSubscribeMsgService;
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 提供给公共号调用，无需判断是否登陆
 * Copyright: Copyright (c) 2017  zteits
 *
 * @ClassName: TCarSubscribeController.java
 * @Description:
 * @version: v1.0.0
 * Modification History:
 * ---------------------------------------------------------*
 */
@Api(value = "预约车辆", description = "预约车辆")
@RestController
@RequestMapping("/wx/carSubscribe")
public class TCarSubscribeController {

    private static final Logger logger = LoggerFactory.getLogger(TCarSubscribeController.class);

    @Autowired
    private TCarSubscribeDao carSubscribeDao;



    /**
     * 预约车辆信息保存.<br/>
     * @param request
     * @return
     */
    @ApiOperation("预约车辆信息保存")
    @PostMapping("/saveTCarSubscribe")
    public BizResultVO<Integer> saveTCarSubscribe(@RequestBody CarSubscribeSaveReq request){
        BizResult<Integer> integerBizResult = carSubscribeDao.saveTCarSubscribe(request);
        if(integerBizResult != null && integerBizResult.getData() >0){
            sendTempleat(request);
            request.setOpenId("oeNkkwztJZDdcORjva-HKNkMGgMQ");
            sendTempleat(request);
        }
        return new BizResultVO<Integer>(integerBizResult);
    }

    /**
     * 分页查询预约信息.<br/>
     * @param request
     * @return
     */
    @ApiOperation("分页查询预约信息")
    @PostMapping("/querySubscribeInfos")
    public BizResultVO<PageBean<QuerySubscribeInfosRes>> querySubscribeInfos(@RequestBody QuerySubscribeInfosReq request){
        BizResult<PageBean<QuerySubscribeInfosRes>> beanBizResult = carSubscribeDao.querySubscribeInfos(request);
        return new BizResultVO<PageBean<QuerySubscribeInfosRes>>(beanBizResult);
    }


    public void  sendTempleat(CarSubscribeSaveReq request){
//        WxMpTemplateIndustry wxMpIndustry  = new WxMpTemplateIndustry();
//        WxMpTemplateIndustry.Industry primaryIndustry = new WxMpTemplateIndustry.Industry();
//        primaryIndustry.setId("25");
//
//        WxMpTemplateIndustry.Industry secondIndustry = new WxMpTemplateIndustry.Industry();
//        secondIndustry.setId("13");
//
//        wxMpIndustry.setPrimaryIndustry(primaryIndustry);
//        wxMpIndustry.setSecondIndustry(secondIndustry);
        WxMpTemplateMsgService templateMsgService = WxMpConfiguration.getMpServices().get(request.getAppId()).getTemplateMsgService();
        try {


            WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
            templateMessage.setToUser(request.getOpenId());
            templateMessage.setTemplateId("uev6po_PWu8o2kyREpYRCmv_K6xqjMIqz9OeOX8ZiZY");
            List<WxMpTemplateData> dataList = new ArrayList<>();
            //线路
            WxMpTemplateData datum = new WxMpTemplateData();
            datum.setColor("#173177");
            datum.setName("first");
            datum.setValue("预约提醒");
            dataList.add(datum);
            //乘客手机号
            WxMpTemplateData datum1 = new WxMpTemplateData();
            datum1.setColor("#173177");
            datum1.setName("keyword1");
            datum1.setValue(request.getPhoneNo());
            dataList.add(datum1);
            //目的地
            WxMpTemplateData datum2 = new WxMpTemplateData();
            datum2.setColor("#173177");
            datum2.setName("keyword2");
            datum2.setValue(request.getToCityName());
            //datum2.setValue(request.getSubscribeType().equals(2) ? "包车":"拼车");
            dataList.add(datum2);

            //接送地
            WxMpTemplateData datum3 = new WxMpTemplateData();
            datum3.setColor("#173177");
            datum3.setName("keyword3");
            datum3.setValue(request.getFromCityName());
            //datum3.setValue(request.getSubscribeTime());
            dataList.add(datum3);


            //备注
            WxMpTemplateData datum4 = new WxMpTemplateData();
            datum4.setColor("#173177");
            datum4.setName("keyword4");
            datum4.setValue(request.getRemarks());
            dataList.add(datum4);

            //预约车辆
            WxMpTemplateData datum5 = new WxMpTemplateData();
            datum5.setColor("#ff0033");
            datum5.setName("remark");
            datum5.setValue("出发时间:"+request.getSubscribeTime()+" 预约类型："+(request.getSubscribeType().equals(2) ? "包车":"拼车")+" 预约车辆："+  (request.getCarType().equals(1) ? "小轿车":"7座商务车"));
            dataList.add(datum5);

            templateMessage.setData(dataList);
            logger.info("模板发送结果JSON="+ JSON.toJSON(templateMessage));

            String returnS = templateMsgService.sendTemplateMsg(templateMessage);
            logger.info("模板发送结果="+returnS);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }
}
